// pages/points/index.js
Page({
  data: {
    totalPoints: 0,
    pointsList: [],
    loading: true,
    isEmpty: false,
    currentPage: 1,
    pageSize: 10,
    finished: false,
    active: 0,
    tabs: [
      { title: '全部', type: '' },
      { title: '获取', type: 'earn' },
      { title: '使用', type: 'use' }
    ],
    currentType: ''
  },

  onLoad: function (options) {
    this.loadPointsData();
  },

  onPullDownRefresh: function () {
    this.resetAndReload();
    wx.stopPullDownRefresh();
  },

  onReachBottom: function () {
    if (!this.data.finished) {
      this.loadMorePoints();
    }
  },

  // 切换标签
  onTabChange: function (event) {
    const index = event.detail.index;
    const type = this.data.tabs[index].type;
    
    this.setData({
      active: index,
      currentType: type,
      currentPage: 1,
      pointsList: [],
      loading: true,
      finished: false
    });
    
    this.loadPointsData();
  },

  // 加载积分数据
  loadPointsData: function () {
    const { currentPage, pageSize, currentType, pointsList } = this.data;
    
    // 显示加载状态
    this.setData({ loading: true });
    
    // 模拟API请求，实际开发中应替换为真实的API调用
    setTimeout(() => {
      // 模拟获取积分数据
      const result = this.getMockPointsData(currentPage, pageSize, currentType);
      
      // 更新数据
      this.setData({
        totalPoints: result.totalPoints,
        pointsList: [...pointsList, ...result.list],
        loading: false,
        isEmpty: currentPage === 1 && result.list.length === 0,
        finished: result.list.length < pageSize,
        currentPage: currentPage + 1
      });
    }, 1000);
  },

  // 加载更多积分记录
  loadMorePoints: function () {
    if (!this.data.loading) {
      this.loadPointsData();
    }
  },

  // 重置并重新加载
  resetAndReload: function () {
    this.setData({
      currentPage: 1,
      pointsList: [],
      loading: true,
      finished: false
    });
    
    this.loadPointsData();
  },

  // 查看积分规则
  viewPointsRules: function () {
    wx.navigateTo({
      url: '/pages/points-rules/index'
    });
  },

  // 模拟获取积分数据
  getMockPointsData: function (page, pageSize, type) {
    const totalPoints = 2580;
    const allPointsList = [];
    
    // 生成模拟数据
    for (let i = 0; i < 30; i++) {
      const isEarn = i % 3 !== 0; // 2/3的概率是获取积分，1/3的概率是使用积分
      const pointType = isEarn ? 'earn' : 'use';
      
      // 如果指定了类型，则只返回该类型的记录
      if (type && type !== pointType) continue;
      
      const date = new Date();
      date.setDate(date.getDate() - i);
      
      const points = isEarn ? Math.floor(Math.random() * 100) + 10 : -(Math.floor(Math.random() * 50) + 5);
      
      allPointsList.push({
        id: `P${10000 + i}`,
        type: pointType,
        points: points,
        description: isEarn ? this.getRandomEarnDesc() : this.getRandomUseDesc(),
        date: this.formatDate(date)
      });
    }
    
    // 分页处理
    const startIndex = (page - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const list = allPointsList.slice(startIndex, endIndex);
    
    return {
      totalPoints: totalPoints,
      list: list
    };
  },

  // 获取随机的积分获取描述
  getRandomEarnDesc: function () {
    const earnDescs = [
      '签到奖励',
      '购物奖励',
      '活动参与',
      '邀请好友',
      '评价商品',
      '完成任务',
      '新人奖励',
      '会员福利'
    ];
    
    return earnDescs[Math.floor(Math.random() * earnDescs.length)];
  },

  // 获取随机的积分使用描述
  getRandomUseDesc: function () {
    const useDescs = [
      '兑换商品',
      '抵扣订单',
      '参与抽奖',
      '兑换优惠券',
      '会员升级',
      '积分过期'
    ];
    
    return useDescs[Math.floor(Math.random() * useDescs.length)];
  },

  // 格式化日期
  formatDate: function (date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    
    return `${year}-${month}-${day}`;
  }
});